//
//  ListSelectView.m
//  DCFAPP
//
//  Created by Tsou on 2017/2/17.
//  Copyright © 2017年 WF. All rights reserved.
//

#import "ListSelectView.h"

@interface ListSelectView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIView *centerView;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *conpleteBtn;
@property (nonatomic,strong)UITableView *listtableView;
@property (nonatomic,strong)UILabel *storeNameLb;
@property (nonatomic,strong)UIButton *downBtn;
@property (nonatomic,copy)NSArray *dataAry;
@property (nonatomic,copy)NSString *title;


@end
@implementation ListSelectView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title dataAry:(NSArray *)dataAry{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.dataAry = dataAry;
        self.title = title;
        [self setUI];
   
    }
    return self;
}
#pragma mark - 移除覆盖和列表（标题）
-(void)removeSelf{
    [UIView animateWithDuration:0.36 animations:^{
        
        [self removeFromSuperview];
    }];

}
//确认
-(void)completeBtnClick{
    if (self.selectBlock) {
        self.selectBlock(self.storeNameLb.text);
    }
    [self removeSelf];
}
-(void)setUI{
    
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = RGBAColor(0, 0, 0, 0.3);
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(20, (SCREEN_HEIGHT-175)/2.0, SCREEN_WIDTH-40, 175)];
    centerView.backgroundColor = WHITECOLOR;
    centerView.layer.cornerRadius = 10;
    [self addSubview:centerView];
    self.centerView = centerView;
   
    //选择title
    UILabel *selectLb = [UILabel new];
    [self.centerView addSubview:selectLb];
    selectLb.frame = CGRectMake(0, 0, self.centerView.mh_width, 50);
    selectLb.text = self.title;
    selectLb.font = BFONT(18);
    selectLb.textAlignment = NSTextAlignmentCenter;
    
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(20, selectLb.mh_bottom+10, self.centerView.mh_width-40, 33)];
    subView.layer.borderColor = BACKGROUNDCOLOR.CGColor;
    subView.layer.borderWidth = 1;
    [self.centerView addSubview:subView];
    UILabel *storeNameLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, subView.mh_width-40, subView.mh_height)];
    storeNameLb.font = FONT(15);
    storeNameLb.textColor = themeTextColor;
    storeNameLb.text = @"西湖银泰店";
    [subView addSubview:storeNameLb];
    self.storeNameLb = storeNameLb;
    UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    downBtn.frame = CGRectMake(subView.mh_width-40, 0, 40, subView.mh_height);
    [downBtn setImage:IMAGE(@"xiala") forState:UIControlStateNormal];
    [subView addSubview:downBtn];
    downBtn.selected = NO;
    [downBtn addTarget:self action:@selector(actionOfDown:) forControlEvents:UIControlEventTouchUpInside];
    self.downBtn = downBtn;
    
    
    //取消
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(0, self.centerView.mh_height-50, self.centerView.mh_width/2.0, 50);
    [self.centerView addSubview:self.cancelBtn];
    self.cancelBtn.titleLabel.font = FONT(18);
    [self.cancelBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
    [self.cancelBtn setTitle:NSLocalizedString(@"cancel", nil) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.cancelBtn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
    
    //确认
    self.conpleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.centerView addSubview:self.conpleteBtn];
    self.conpleteBtn.frame = CGRectMake(self.centerView.mh_width/2.0, self.centerView.mh_height-50, self.centerView.mh_width/2.0, 50);
    self.conpleteBtn.titleLabel.font = FONT(18);
    [self.conpleteBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
    [self.conpleteBtn setTitle:NSLocalizedString(@"sure", nil) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.conpleteBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //线
    [QTTools tool_addLineWithFrame:CGRectMake(0, self.centerView.mh_height-50, self.centerView.mh_width, 1) superView:self.centerView];
    [QTTools tool_addLineWithFrame:CGRectMake(self.centerView.mh_width/2.0, self.centerView.mh_height-50, 1, 50) superView:self.centerView];
    
    CGFloat height = self.dataAry.count*44;
    if (height>176) {
        height = 176;
    }
    self.listtableView = [[UITableView alloc] initWithFrame:CGRectMake(self.centerView.mh_left+10, self.centerView.mh_top+93, self.centerView.mh_width-20, height) style:UITableViewStylePlain];
    self.listtableView.delegate = self;
    self.listtableView.dataSource = self;
    self.listtableView.backgroundColor = WHITECOLOR;
    [self addSubview:self.listtableView];
    self.listtableView.hidden = YES;
    
}
-(void)actionOfDown:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.listtableView.hidden = NO;
    }else{
        
        self.listtableView.hidden = YES;
    }
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataAry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
     NSString *name = self.dataAry[indexPath.row];
    cell.textLabel.text = name;
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.textColor = [UIColor blackColor];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *name = self.dataAry[indexPath.row];
    self.storeNameLb.text = name;
    self.listtableView.hidden = YES;
    self.downBtn.selected = NO;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
